/**
 * $Id$
 *
 * Copyright (c) 2014 ChinaSoft International, Ltd. All rights reserved
 * JointForce Project
 *
 */
package com.csi.jf.crucible.api.crucible;

import java.io.IOException;
import java.util.Iterator;

import com.chinasofti.ro.bizframework.modules.json.JSONArray;
import com.chinasofti.ro.bizframework.modules.json.JSONObject;
import com.icss.j2ee.util.http.HttpClientException;

/**
 * @Title: ApiTestProject.java
 * @Description: <br>
 *               <br>
 * @Company: Chinasofti
 * @Created on 2014-7-4 下午3:25:41
 * @author zhangmh
 * @version $Revision: 1.0 $
 * @since 1.0
 */
public class ApiTestProject {

    private RestClient client = new RestClient();
    
    private static String key = "newkey";
    private static String name = "newproject";
    
    public static void main(String[] args) throws Exception{
        ApiTestProject test = new ApiTestProject();
        test.testProjectCreateApi();
    }
    
    /**
     * create project
     * @throws HttpClientException
     * @throws IOException
     * @return
     * {
           "response": {
                    "code": "200", 
                    "message": "operation succeeded", 
                    "cause": ""
           }
       }
     */
    public void testProjectCreateApi() throws HttpClientException, IOException{
        //name={projectName}&key={projectKey}&defaultRepositoryName={defaultRepoName}&storeRevisions={true/false}&permissionSchemeName={permissionScheme}&moderatorEnabled={true/false}&defaultModerator={defaultModerator}&defaultDuration={defaultDuration}&defaultObjectives={defaultObjective}
        String param="name="+name+"&key="+key+"&defaultRepositoryName=CrucibleTest&storeRevisions=true&permissionSchemeName=agile&moderatorEnabled=true&defaultModerator=zhaochen&defaultDuration=8&defaultObjectives=do-new-review";
        String url = client + "/rest/project-admin/latest/project-new"+param;
        client.doGet(url);
    }
    
    public void createProject()throws Exception{
        //Adds repository
        String url = "/rest-service-fecru/project-admin/latest/project-new?name=JfPts0703&key=PTS&defaultRepositoryName=CrucibleTest&storeRevisions=true&permissionSchemeName=agile&moderatorEnabled=true&defaultModerator=admin&defaultDuration=145&defaultObjectives=MyObjective&SONAR_URL=https://zhangmh%40chinasofti.com@git.jointforce.com/scm/join/jfpts0703.git&username=zhangmh&password=zhangmh2013";
        client.doGet(url);
    }
    
    /**
     * update project
     * @throws HttpClientException
     * @throws IOException
     * @return
     * {
                "response": {
                    "code": "200", 
                    "message": "operation succeeded", 
                    "cause": ""
                }
            }
     */
    public void testProjectUpdateApi() throws HttpClientException, IOException{
        //"name={projectName}&key={projectKey}&defaultRepositoryName={defaultRepoName}&storeRevisions={true/false}&permissionSchemeName={permissionScheme}&moderatorEnabled={true/false}&defaultModerator={defaultModerator}&defaultDuration={defaultDuration}&defaultObjectives={defaultObjective}";
        String param="name="+name+"&key="+key+"&defaultRepositoryName=CrucibleTest&storeRevisions=true&permissionSchemeName=agile&moderatorEnabled=true&defaultModerator=zhangmh&defaultDuration=5&defaultObjectives=do-test-review";
        String url = "/rest/project-admin/latest/project-update"+param;
        client.doGet(url);
    }
    
    /**
     * list project
     * @throws HttpClientException
     * @throws IOException
     * @return
     * {
            "response": {
                "code": "200", 
                "message": "operation succeeded", 
                "cause": ""
            }, 
            "projectList": [
                {
                    "name": "Default Project", 
                    "key": "CR", 
                    "storeRevisions": true, 
                    "permissionSchemeId": 2, 
                    "moderatorEnabled": true, 
                    "defaultDuration": 5
                }, 
                {
                    "name": "PtsTestProject", 
                    "key": "PTS-TEST", 
                    "defaultRepositoryName": "CrucibleTest", 
                    "storeRevisions": true, 
                    "permissionSchemeId": 2, 
                    "moderatorEnabled": true, 
                    "defaultModerator": "zhangmh", 
                    "defaultDuration": 7, 
                    "defaultObjectives": ""
                }
            ]
        }
     */
    public void testProjectListApi() throws HttpClientException, IOException{
        String url = "/rest/project-admin/latest/project-list";
        client.doGet(url);
    }
    
    /**
     * delete project
     * @throws HttpClientException
     * @throws IOException
     * @return
     * {
                "response": {
                    "code": "200", 
                    "message": "operation succeeded", 
                    "cause": ""
                }
            }
     */
    public void testProjectAdminApi() throws HttpClientException, IOException{
        //http://crucible.mycompany.com/rest/project-admin/latest/project-delete?key=MNP
        String url = "/rest/project-admin/latest/project-delete?key="+key;
        client.doGet(url);
    }
    
    /**
     * 判断project是否已经在crucible中存在
     * 
     * @param projectName
     * @return
     * @Created on 2014-7-4 下午4:33:39
     * @author FengHaiBing
     */
    public boolean isExistProject(String projectName) {
    	if(projectName == null) return false;
        //list project
        String url = "/rest/project-admin/latest/project-list" ;
        try {
        	String restReturn = client.doGet(url);
        	if(restReturn == null) return false;
            /*
            {
                "response": {
                    "code": "200", 
                    "message": "operation succeeded", 
                    "cause": ""
                }, 
                "projectList": [
                    {
                        "name": "Default Project", 
                        "key": "CR", 
                        "storeRevisions": true, 
                        "permissionSchemeId": 2, 
                        "moderatorEnabled": true, 
                        "defaultDuration": 5
                    }, 
                    {
                        "name": "PtsTestProject", 
                        "key": "PTS-TEST", 
                        "defaultRepositoryName": "CrucibleTest", 
                        "storeRevisions": true, 
                        "permissionSchemeId": 2, 
                        "moderatorEnabled": true, 
                        "defaultModerator": "zhangmh", 
                        "defaultDuration": 7, 
                        "defaultObjectives": ""
                    }
                ]
            }
            */
        	
        	JSONObject jo = JSONObject.fromObject(restReturn);
        	if(jo == null || jo.get("projectList") == null) return false;
        	
        	JSONArray ja = (JSONArray)jo.get("projectList");
        	
        	Iterator<JSONObject> it = ja.iterator();
        	while (it.hasNext()) {
        		JSONObject ne = it.next();
        		String pn = (ne.get("name") == null ? "" : ne.get("name").toString() );
        		if(projectName.equalsIgnoreCase(pn)) return true;
        	}
		} catch (HttpClientException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    	
    	return false;
    }
    
    /**
     * 在crucible中创建一个project
     * 
     * @param projectName
     * @param projectKey
     * @param repoName
     * @param moderator
     * @Created on 2014-7-4 下午5:01:12
     * @author FengHaiBing
     */
    public void createProject(String projectName, String projectKey, 
    		String repoName, String moderator) {
    	if(isExistProject(projectName)) return;
    	if(projectKey == null || projectKey.trim().equals("")) return ;
//        String key = "newkey";
//        String name = "newproject";
        //create project
        String param="name="+projectName+"&key="+projectKey+"&defaultRepositoryName="+repoName+"&storeRevisions=true&permissionSchemeName=agile&moderatorEnabled=true&defaultModerator=zhaochen&defaultDuration=8&defaultObjectives=do-new-review";
        String url = "/rest/project-admin/latest/project-new?" + param;
        try {
        	client.doGet(url);
		} catch (HttpClientException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
        /*
         * {
                "response": {
                    "code": "200", 
                    "message": "operation succeeded", 
                    "cause": ""
                }
            }
         */
    	
    }
    

}
